package test;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author lichao 
 * 启动Spring组件
 */
public class SpringUtils {

	private SpringUtils() {
		init();
	}

	private static class Holder {
		static final SpringUtils INSTANCE = new SpringUtils();
	}

	public static SpringUtils getInstance() {
		return Holder.INSTANCE;
	}

	private ApplicationContext applicationContext;

	private void init() {
		try {
			applicationContext = new ClassPathXmlApplicationContext("classpath*:application-context.xml");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Object getBean(String name) {
		return applicationContext.getBean(name);
	}
	
	public <T> T getBean(Class<T> requiredType) {
		return applicationContext.getBean(requiredType);
	}
	
	public <T> T getBean(String name, Class<T> requiredType) throws BeansException {
		return applicationContext.getBean(name, requiredType);
	}

	// /**
	// * 启动spring
	// */
	// public static void main(String[] args) {
	// SpringUtils.getInstance();
	// }
}
